Func _BlackCon_ConnectToServer ($sName, $sPassword)
	_Write ("_BlackCon_ConnectToServer (" & $sName & "," &  $sPassword & ")")

	_GUI_DrawWallpaper ()
	_GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0)


	Global $hServerSocket = TCPConnect($SERVER_IP, $SERVER_Port)
	_Write ("Create socket : " & $SERVER_IP & ":" & $SERVER_Port)
	_BlackCon_DrawConnectionState("Create socket...")
	If @error = 1 Then
		_Write ("Cant create socket (wrong IP) @error : " & @error, 1)
		_BlackCon_DrawConnectionState("The IP Adress is incorrect!" & @CRLF & "Please reinstall game." & @CRLF & "Press [ENTER]")
		Do
			Sleep(100)
		Until _IsPressed("0D")
		Exit
	ElseIf @error = 2 Then
		_Write ("Cant create socket (wrong port) @error : " & @error, 1)
		_BlackCon_DrawConnectionState("The Port is incorrect!" & @CRLF & "Please reinstall game." & @CRLF & "Press [ENTER]")
		Do
			Sleep(100)
		Until _IsPressed("0D")
		Exit
	ElseIf @error = 10061 Then
		_Write ("Cant create socket (server down) @error : " & @error, 2)
		_BlackCon_DrawConnectionState("Cant connect to server @ " & $SERVER_IP & ":" & $SERVER_Port & @CRLF & "Press [ENTER]")
		Do
			Sleep(100)
		Until _IsPressed("0D")
	ElseIf $hServerSocket = -1 Or $hServerSocket = 0 Then
		_Write ("Cant create socket (server down) @error : " & @error, 2)
		_BlackCon_DrawConnectionState("Cant connect to server @ " & $SERVER_IP & ":" & $SERVER_Port & @CRLF & "Press [ENTER]")
		Do
			Sleep(100)
		Until _IsPressed("0D")
	Else
		$bIsConnected = True
		_BlackCon_SendPacket("log|" & $CLIENT_IP & "|" & $GAME_VERS & "|" & $sName & "|" & $sPassword)
		_Setting_Save ("ACC_Name", $sName)
		_Setting_Save ("ACC_Password", $sPassword)
		_Write ("Sending data to server...")
		_BlackCon_DrawConnectionState("Sending account data to server...")
		$hRecvTimer = TimerInit()
		Local $sRecv = TCPRecv($hServerSocket, 2048)
		_BlackCon_DrawConnectionState("Wait for server to reply")
		While 1
			$sRecv = TCPRecv($hServerSocket, 2048)
			If $sRecv <> "" Then
				$sRecv = StringSplit($sRecv, "|")
				If $sRecv[1] = "login" And $sRecv[2] = "ok" Then
					_BlackCon_DrawConnectionState("Login data was correct")
					_BlackCon_SendPacket ("getmap|data")
					_BlackCon_DrawConnectionState("Get Map Data")
					_Filecreate("Map_Cache\Data.dat")
					_Write ("_FileCreate('Map_Cache\Data.dat')")
					_Filecreate("Map_Cache\Map.wws")
					_Write ("_Filecreate('Map_Cache\Map.wws')")
					_Write ("Waiting for server to reply")
					While 1
						$sRecv = TCPRecv($hServerSocket, 4096 * 100)
						If $sRecv <> "" Then
							FileWrite("Map_cache\Data.dat", $sRecv)
							ExitLoop
						EndIf
					WEnd
					_BlackCon_SendPacket ("getmap|map")
					_Write ("Waiting for server to reply")
					While 1
						$sRecv = TCPRecv($hServerSocket, 8192)
						If $sRecv <> "" Then
							FileWrite("Map_cache\Map.wws", $sRecv)
							ExitLoop
						EndIf
					WEnd
					$sServerNews = "WISH - 0.1.0" & @CRLF & "Welcome to my game called WISH." & @CRLF & "Its a fantasy MMORPG where you" & @CRLF & "have to kill other players" & @CRLF & "by using your spells."
					_StartMenu_MenuLoop ()
				ElseIf $sRecv[1] = "login" And $sRecv[2] = "wrong" Then
					_Write ("There was an error : " & $sRecv[2], 1)
					MsgBox(0, "", "Error : " & $sRecv[2])
					_BlackCon_LoginScreen ()
					ExitLoop
				ElseIf $sRecv[1] = "update" Then
					_BlackCon_DrawConnectionState("Your game version is incorrect." & @crlf & "Downloading required version now.")
					Sleep(1000)
					$sUpdateName = $sRecv[2]
					$sUpdateURL = $sRecv[3]
					_Write ("Update name is : " & $sUpdateName)
					_BlackCon_SendPacket("updt|get")
					$bUpdateData = ""
					While 1
						$sRecv = TCPRecv($hServerSocket, 8192)
						If $sRecv <> "" Then
							FileWrite("" & $sUpdateName & ".exe", $sRecv)
							_Write ("Still Loading...")
							_BlackCon_DrawConnectionState("Loading update", $hBuffer)
						ElseIf @error = -1 Then
							_Write ("Loaded update...")
							ExitLoop
						EndIf
					WEnd



					_Write ("Run update now...")
					MsgBox(0, "Update", "The Update was loaded." & @CRLF & "Game will exit and the update will start.")
					Run(@ScriptDir & "\" & $sUpdateName & ".exe")
					If @error Then _Write ("There was an error running the update " & @error, 1)
					Exit
;~ 					StringReplace(FileRead("updates\" & $sRecv[2] & ".exe"),"Update_exe_ends_here", "")
;~ 					_BlackCon_LoginScreen ()
				EndIf
			EndIf
			If TimerDiff($hRecvTimer) >= 5000 Then
				_Write ("Cant reach server...", 2)
				_BlackCon_DrawConnectionState("Server needs too long to reply @ " & $SERVER_IP & ":" & $SERVER_Port & @CRLF & "Press [ENTER]")
				Do
					Sleep(100)
				Until _IsPressed("0D")
				_BlackCon_LoginScreen ()
				ExitLoop
			EndIf
;~ 			_GDIPlus_GraphicsDrawImageRect($hBuffer, $hWallpaperImage, 0, 0, 800, 600)
;~ 			_Mouse_DrawCursor ()
;~ 			_GUI_Render ()
		WEnd
	EndIf
EndFunc

Func _BlackCon_LoginScreen ()
	_Write ("_BlackCon_LoginScreen ()")
	GUISetState(@SW_SHOW, $hGUI)
	$sPassword = _Setting_Load ("ACC_Password", "Password")
	$sName = _Setting_Load ("ACC_Name", "Name")
	$sServerInfoText = FileReadLine ("data\BlackCon\server.dat", 3)
	$hFPSTimer = TimerInit()
	$i_FPS = 0
	$iFPS = 0
	_Write ("Start Loop")
	Do
		$sCryptPassword = ""
		$aSplitPassword = StringSplit($sPassword, "")
;~ 		If IsArray("aSplitPassword") Then
			For $i = 1 To $aSplitPassword[0]
				$sCryptPassword &= "*"
			Next
;~ 		EndIf
		_GUI_DrawWallpaper ()

		_GUI_DrawWindow("Login", 800 / 2 - 300 / 2, 600 / 2 - 100 + 30, 300, 200)

		_GDIPlus_GraphicsDrawRect($hBuffer, 350, 295, 100, 14)
		_GUI_Drawstring($sName, 350, 295)
		_GDIPlus_GraphicsDrawRect($hBuffer, 350, 315, 100, 14)
		_GUI_Drawstring($sCryptPassword, 350, 315)

		_GUI_Drawstring($sServerInfoText, 10, 578 - 12)

		_GUI_DrawButton ("Register", 10, 10)
		_GUI_DrawButton("Login", 350, 333)
		_Mouse_DrawCursor ()
;~ 		_GUI_Drawstring("WISH - Beta - " & $GAME_VERS & @CRLF & "Build: Dethroned" & @CRLF & "FPS : " & $iFPS, 0, 0)
		_GUI_Drawstring($GAME_VERS, 10, 578)
		_GUI_Render ()
		If _Mouse_CheckClickedArea (350, 295, 100, 14) Then
			$sName = InputBox("Login", "Please enter your nickname!")
		ElseIf _Mouse_CheckClickedArea (350, 315, 100, 14) Then
			$sPassword = InputBox("Login", "Please enter your password!", $sPassword, "*")
		ElseIf _Mouse_CheckClickedArea (350, 333, 100, 25) Then
			Global $PLAYER_NAME = $sName
			Global $PLAYER_PASSWORD = $sPassword
			_BlackCon_DrawConnectionState("Try to connect  @ " & $SERVER_IP & ":" & $SERVER_Port)
			_BlackCon_ConnectToServer ($sName, $sPassword)
		ElseIf _Mouse_CheckClickedArea (10, 10, 100, 25) Then
			_BlackCon_Register ()
		EndIf
		$i_FPS += 1
		If TimerDiff($hFPSTimer) >= 1000 Then
			$iFPS = $i_FPS
			$i_FPS = 0
			$hFPSTimer = TimerInit ()
		EndIf
	Until GUIGetMsg() = -3
	_Write ("GUI close action -> exit")
	Exit
EndFunc

Func _BlackCon_Register ()
	MsgBox(0, "", "Write a mail to WISH on facebook if you want to get an account." & @crlf & "Otherwise use beta as your password and an random name to play without an account.")
EndFunc

Func _BlackCon_SendPacket($sPacket)
	_Write ("_BlackCon_SendPacket(" & $sPacket & ")")
	TCPSend($hServerSocket, StringToBinary($sPacket))
EndFunc

Func _BlackCon_DrawConnectionState_old($sString, $hDrawSurface = $hGraphics)
	_GDIPlus_GraphicsFillRect($hDrawSurface, 200, 200, 400, 200, $hButtonBrush)
	_GDIplus_GraphicsDrawString($hDrawSurface, $sString, 295, 295)
	_GDIPlus_GraphicsDrawRect($hDrawSurface, 200, 200, 400, 200)
EndFunc

Func _BlackCon_DrawConnectionState($sString, $hDrawSurface = $hGraphics)
	_GUI_DrawWindow ("Connecting...", 200, 200, 400, 200)
	_GDIplus_GraphicsDrawString($hBuffer, $sString, 295, 295)
	_GDIPlus_GraphicsDrawImage($hDrawSurface, $hBitmap, 0, 0)
EndFunc

Func _BlackCon_Shutdown ()
	_Write ("_BlackCon_Shutdown ()")
	If IsDeclared("$hServerSocket") Then
		TCPCloseSocket($hServerSocket)
		If @error Then _Write ("Error closing socket : " & @error)
	EndIf

EndFunc